import {_decorator, Component, Label, Node, Sprite, SpriteFrame} from 'cc';
import {SkillsController} from "db://assets/scripts/ui/pause/SkillsController";
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {PriorityEnum} from "db://assets/scripts/game/PriorityEnum";
import {ResourceUtils} from "db://assets/scripts/common/ResourceUtils";
import {GameManager} from "db://assets/scripts/game/GameManager";
import {UIManager} from "db://assets/scripts/common/UIManager";
import {EventManager} from "db://assets/scripts/common/EventManager";
import {PlayerDataManager} from "db://assets/scripts/common/PlayerDataManager";
import {Skill} from "db://assets/scripts/data/model/Skill";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";
import {EventType} from "db://assets/scripts/common/EventType";
import {GameState} from "db://assets/scripts/game/GameState";

const {ccclass, property} = _decorator;

/**
 * 暂停界面
 */
@ccclass('PauseController')
export class PauseController extends Component {
    /**
     * 技能图标
     */
    @property(Sprite)
    public skillIconSprite: Sprite;
    /**
     * 技能名称
     */
    @property(Label)
    public skillNameLabel: Label;
    /**
     * 技能描述
     */
    @property(Label)
    public skillDescriptionLabel: Label;
    /**
     * 技能列表
     */
    @property(Node)
    public skillsNode: Node;
    /**
     * 技能
     */
    @property(Node)
    public skillItemNode: Node;
    /**
     * 音乐（开）图片
     */
    @property(SpriteFrame)
    public musicOnSF: SpriteFrame;
    /**
     * 音乐（关）图片
     */
    @property(SpriteFrame)
    public musicOffSF: SpriteFrame;
    /**
     * 声音
     */
    @property(Sprite)
    public soundSprite: Sprite;
    /**
     * 是否开启音乐
     */
    private isMusicOpen: boolean = false;

    /**
     * 显示暂停界面
     */
    public show() {
        // 获取玩家所拥有的技能
        let skillIds = PlayerDataManager.getInstance().getPlayer().skillIds;

        // 当拥有的技能不为空时
        if (skillIds.length === 0) {
            // 冻结节点
            this.skillItemNode.active = false;
            this.skillsNode.active = false;
        } else {
            // 激活节点
            this.skillItemNode.active = true;
            this.skillsNode.active = true;

            let skillsController = this.skillsNode.getComponent(SkillsController);

            skillsController.init((playerSkill: Skill) => {
                this.skillNameLabel.string = playerSkill.name;
                this.skillDescriptionLabel.string = playerSkill.description;
                ResourceUtils.setSpriteFrame(`texture/skillIcon/${playerSkill.icon}`, this.skillIconSprite);
            });
        }

        this.isMusicOpen = AudioManager.getInstance().getMusicEnable();
        this.changeState();
    }

    /**
     * 改变声音的图片
     */
    public changeState() {
        if (this.isMusicOpen) {
            this.soundSprite.spriteFrame = this.musicOnSF;
        } else {
            this.soundSprite.spriteFrame = this.musicOffSF;
        }
    }

    /**
     * 切换是否开启声音
     */
    public onSound() {
        AudioManager.getInstance().playSound(AudioPaths.clickSound);

        this.isMusicOpen = !this.isMusicOpen;
        this.changeState();

        if (this.isMusicOpen) {
            AudioManager.getInstance().openMusic();
            AudioManager.getInstance().openSound();
        } else {
            AudioManager.getInstance().closeMusic();
            AudioManager.getInstance().closeSound();
        }
    }

    /**
     * 返回主界面
     */
    public onBackHome() {
        // 播放声音
        AudioManager.getInstance().playSound(AudioPaths.clickSound);

        // 显示黑屏
        UIManager.getInstance().showDialog("back/backPanel", [() => {
            // 关闭战斗界面
            UIManager.getInstance().hideDialog("fight/fightPanel");
            // 关闭暂停界面
            UIManager.getInstance().hideDialog("pause/pausePanel");
            // 游戏结束
            GameManager.getInstance().gameStateProxy.setGameState(GameState.OVER);
            // 分发事件
            EventManager.dispatchEvent(EventType.RECYCLE_ALL);
            // 显示主界面
            UIManager.getInstance().showDialog("home/homePanel");
        }], null, PriorityEnum.WAITING);
    }

    /**
     * 继续游戏
     */
    public onPlay() {
        // 播放声音
        AudioManager.getInstance().playSound(AudioPaths.clickSound);
        // 隐藏暂停界面
        UIManager.getInstance().hideDialog("pause/pausePanel");
        // 继续游戏
        GameManager.getInstance().gameStateProxy.setGameState(GameState.PLAYING);
    }
}